<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 9/19/14
 * Time: 9:28 PM
 */

namespace Backend\Modules\Video\Controllers;

use \Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\Model\Resultset;
use Library\Pagination\Pager as Pager;
use Backend\Modules\Video\Models\Video;
use Backend\Modules\Video\Models\VideoDescription;
use Backend\Modules\Video\Models\VideoCategoryLink;
use Backend\Modules\Video\Forms\FormCreateVideo;

class VideoController extends \BackendController
{
    public $urlVideo = 'video';

    public function indexAction()
    {
        $currentPage = abs($this->request->getQuery('page', 'int', 1));
        $video = Video::find();
            
        if ($currentPage == 0) {
            $currentPage = 1;
        }

        $pager = new Pager(
            new Paginator(array(
                'data'  => $video,
                'limit' => 5,
                'page'  => $currentPage,
            )),
            array(
                // We will use Bootstrap framework styles
                'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                // Range window will be 5 pages
                'rangeLength' => 5,
                // Just a string with URL mask
                'urlMask'     => '?page={%page_number}',               
            )
        );
        $this->view->pager = $pager;       
    }

    public function createAction()
    {
        $this->assets
            ->addCss('vendor/jquery-file-upload/css/jquery.fileupload.css')
            ->addCss('vendor/jquery-file-upload/css/jquery.fileupload-ui.css')
            ->addCss('vendor/bootstrap-tagsinput/dist/bootstrap-tagsinput.css')

            ->addJs('vendor/jquery-file-upload/js/vendor/jquery.ui.widget.js')
            //->addJs('http://blueimp.github.io/JavaScript-Load-Image/js/load-image.all.min.js')
            //->addJs('http://blueimp.github.io/JavaScript-Canvas-to-Blob/js/canvas-to-blob.min.js')
            ->addJs('vendor/jquery-file-upload/js/jquery.iframe-transport.js')
            ->addJs('vendor/jquery-file-upload/js/jquery.fileupload.js')
            ->addJs('vendor/jquery-file-upload/js/jquery.fileupload-process.js')
            ->addJs('vendor/jquery-file-upload/js/jquery.fileupload-image.js')
            ->addJs('vendor/jquery-file-upload/js/main.js')
            ->addJs('vendor/editor/ckeditor/ckeditor.js')       
            ->addJs('vendor/typeahead/typeahead.bundle.js')          
            ->addJs('vendor/bootstrap-tagsinput/dist/bootstrap-tagsinput.js')   
            ->addJs('vendor/bootstrap-tagsinput/dist/bootstrap-tagsinput-angular.js')         
            ->addJs('vendor/backend/js/video/controller/video.js');

        $form = new FormCreateVideo();

        if($this->request->isPost())
        {
            if($form->isValid($this->request->getPost()) != false)
            {
                //Check if the user has uploaded files
                if ($this->request->hasFiles() == true) {
                    //Print the real file names and their sizes
                    foreach ($this->request->getUploadedFiles() as $file){
                        echo $file->getName(), " ", $file->getSize(), "\n";
                    }
                }

                $video = new Video();
                //$form->bind($this->request->getPost() , $video);
                $video->assign(array(
                    'imdb' => 0 ,
                    'national_id' => $this->request->getPost('national_id'),
                    'duration' => $this->request->getPost('duration'),
                    'year_product' => $this->request->getPost('year_product'),
                    'year_publish' => $this->request->getPost('year_publish'),
                    'is_vip' => 'N',//$this->request->getPost('is_vip'),
                    'hot_month' => $this->request->getPost('hot_month'),
                    'total_view' => $this->request->getPost('total_view'),
                    'created_user' => $this->request->getPost('national_id'),
                    'published_user' => $this->request->getPost('national_id'),
                    'created_time' => date('Y-m-d h:m:s'),
                    'updated_time' => date('Y-m-d h:m:s'),
                    'ip_address'    => '0',
                    'status' => $this->request->getPost('status'),
                ));
                if($video->save())
                {
                    $videoDescription = new VideoDescription();
                    $videoDescription->assign(array(
                        'video_id'  => $video->getId(),
                        'name'      => $this->request->getPost('name'),
                        'name_vn'      => $this->request->getPost('name'),
                        'short_name'      => $this->request->getPost('short_name'),
                        'description'   => $this->request->getPost('description'),
                    ));
                    $videoDescription->save();

                    $videoCategoryLink = new VideoCategoryLink();
                    $videoCategoryLink->assign(array(
                        'video_id'  => $video->getId(),
                        'category_id'      => $this->request->getPost('category_id'),
                        'position'      => 0,
                    ));
                    $videoCategoryLink->save();

                    return $this->response->redirect($this->getBackendUrl() . '/' . $this->urlVideo);
                }
                $this->flash->error($video->getMessages());
            }
        }
        $this->view->form = $form;
    }
} 